#!/usr/bin/envpython
# -*-coding:UTF-8-*-
'''
@File    :   slash_strip_handle.py
@Contact :   308711822@qq.com
@License :   (C) Copyright 2021-2225, Personal exclusive right.

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/2/1 10:33   小钟同学      1.0         None
'''
import http
import typing

from starlette import status
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import Response
from starlette.types import ASGIApp, Message


class DisconnectedMiddleware(BaseHTTPMiddleware):
    def __init__(self, app: ASGIApp):
        super().__init__(app)

    async def dispatch(self, request, call_next):
        try:
            return await call_next(request)
        except RuntimeError as e:
            if await request.is_disconnected() and str(e) == "No response returned.":
                return Response(content='request is_disconnected', status_code=status.HTTP_204_NO_CONTENT)
            else:
                raise

